﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

using Prism.Games.GalEngine.Scripts.Data.BranchSystem;

namespace Prism.Games.GalEngine.Scripts.Parser
{
    public class SelectorParser : ISceneParser
    {
        public SelectorParser()
        {
            this.SelectorLB = Helper.GraphicBase.FindName(Helper.GraphicBase.GalUIFrame.GamingFrame, "SelectorLB") as ListBox;
            this.GameLayoutGrid = Helper.GraphicBase.FindName(Helper.GraphicBase.GalUIFrame.GamingFrame, "SelectorGrid") as Grid;
        }

        public void Restart()
        {
            this.SelectorLB.Items.Clear();
            this.IsInSelectorMode = false;

            foreach (var i in Helper.Script.Resource.Roles)
            {
                i.Cohesion = 0;
            }
        }

        public void PresentObject(FunctionModel.SceneObject element)
        {
            if (element == null)
                return;

            if (element is Selector)
            {
                var selector = element as Selector;
                if (selector.Options.Count == 0)
                {
                    this.SelectorLB.Items.Clear();
                }
                else
                {
                    foreach (var i in selector.Options)
                    {
                        this.SelectorLB.Items.Add(i);
                    }

                    this.IsInSelectorMode = true;
                    this.GameLayoutGrid.Background = new System.Windows.Media.SolidColorBrush() { Color = System.Windows.Media.Colors.Black, Opacity = 0.5 };
                }
            }
        }

        public void ExecuteOption(Option option)
        {
            if (option != null)
            {
                foreach (var i in option.Results)
                {
                    var q = from j in Helper.Script.Resource.Roles
                            where j.Id == i.Role
                            select j;
                    if (q.Count() == 1)
                    {
                        var role = q.Single();
                        role.Cohesion += i.CohesionDeviation;
                    }
                }

                this.SelectorLB.Items.Clear();
                this.IsInSelectorMode = false;
                this.GameLayoutGrid.Background = new System.Windows.Media.SolidColorBrush() { Color = System.Windows.Media.Colors.Transparent, Opacity = 1 };

                Helper.Parser.InSelectorModeStateNext();

                this.IsJustExecuteOption = true;
            }
        }

        public bool IsInSelectorMode { get; set; }

        public bool IsJustExecuteOption { get; set; }

        public ListBox SelectorLB { get; set; }

        public Grid GameLayoutGrid { get; set; }
    }
}
